import request from '@/utils/request'
import uniFiler from '@/components/libs/uni-filer.js'

export default {
  // 查询列表(分页)
  queryListByPage (data, options) {
    return request({
      url: '/work-order/list/page',
      method: 'get',
      data: data
    })
  },
  // 查询列表
  queryList (data, options) {
    return request({
      url: '/work-order/list/all',
      method: 'get',
      data: data
    })
  },
  // 查询单个
  queryItem (data, options) {
    return request({
      url: '/work-order/item',
      method: 'get',
      data: data
    })
  },
  // 添加
  insert (data) {
    return request({
      url: '/work-order/item',
      method: 'put',
      data: data
    })
  },
  // 更新
  update (data, options) {
    return request({
      url: '/work-order/item',
      method: 'post',
      data: data
    })
  },
  // 删除
  delete (data, options) {
    return request({
      url: '/work-order/item',
      method: 'delete',
      data: data
    })
  }
}

// 用户密码重置
export function resetUserPwd(userId, password) {
  const data = {
    userId,
    password
  }
  return request({
    url: '/system/user/resetPwd',
    method: 'put',
    data: data
  })
}

// 批量重置密码用户
export function batchResetPwd(userId, password) {
  const data = {
    password
  }
  return request({
    url: '/system/user/batchResetPwd/' + userId,
    method: 'get',
    params: data
  })
}

// 用户状态修改
export function changeUserStatus(userId, status) {
  const data = {
    userId,
    status
  }
  return request({
    url: '/system/user/changeStatus',
    method: 'put',
    data: data
  })
}

// 查询用户个人信息
export function getUserProfile() {
  return request({
    url: '/system/user/profile',
    method: 'get'
  })
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return request({
    url: '/system/user/profile',
    method: 'put',
    data: data
  })
}

// 用户密码重置
export function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword
  }
  return request({
    url: '/system/user/profile/updatePwd',
    method: 'put',
    params: data
  })
}

// 用户头像上传
export function uploadAvatar({ params, vm }) {
  return uniFiler.uploadFile({
    url: vm.vuex_config.baseUrl + '/system/user/profile/avatar',
    params,
    vm
  })
}

// 用户电子签名上传
export function uploadSign({ params, vm }) {
  return uniFiler.uploadFile({
    url: vm.vuex_config.baseUrl + '/system/user/profile/uploadSign',
    params,
    vm
  })
}

// 查询授权角色
export function getAuthRole(userId) {
  return request({
    url: '/system/user/authRole/' + userId,
    method: 'get'
  })
}

// 保存授权角色
export function updateAuthRole(data) {
  return request({
    url: '/system/user/authRole',
    method: 'put',
    params: data
  })
}

// 保存授权角色
export function login(data) {
  return request({
    url: '/loginByPassword',
    method: 'post',
    data: data
  })
}

// 保存授权角色
export function logout(data) {
  return request({
    url: '/logout',
    method: 'get',
    data: data
  })
}

// 查询用户个人信息
export function getInfo() {
  return request({
    url: '/getInfo',
    method: 'get'
  })
}

// 同步用户图片
export function syncUserInfo(user, users, $u, view) {
	return new Promise(function(resolve, reject){
		let config = view.vuex_config
		let d1 = null, d2 = null
		// 头像
		if (user.avatar) {
			d1 = uniFiler.fetchImageToBase64(config.baseUrl + user.avatar).then(base64 => {
				return uniFiler.base64ToPath(base64, 'avatar_' + user.userId + '.' + user.avatar.replace(/.+\./, ''))
			}).then(path => {
				user.avatarLocalPath = path
			})
		}
		// 签名
		if (user.signUrl) {
			d2 = uniFiler.fetchImageToBase64(config.baseUrl + user.signUrl).then(base64 => {
				return uniFiler.base64ToPath(base64, 'sign_' + user.userId + '.' + user.signUrl.replace(/.+\./, ''))
			}).then(path => {
				user.signLocalPath = path
			})
		}
		Promise.allSettled([d1, d2]).then(() => {
			users = users || []
			let one = users.find(item => item.userName === user.userName)
			if (one) {
				Object.assign(one, user)
			}
			$u.vuex('vuex_user', { userName: user.userName })
			$u.vuex('vuex_users', users)
			resolve()
		})
	})
}
